/*
 * main.c
 *
 *   Copyright 2010 Emediate ApS
 *
 *   This file is part of Stampede.
 *
 *   Stampede is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Stampede is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Stampede.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   Written by: Erland Lewin <erland@emediate.se>
 */
#include <assert.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

#include "config.h"
#include "webTest.h"

#define FALSE 0

static void usage();

int main( int argc, char **argv )
{
   /*
    * Read input files, and number of simultaneous users
    * For each simultaneous user, choose a session and start it.
    * Whenever a session ends, start a new one.
    *
    */

   /*
    * parse options
    */
   static struct option long_options[] =
     {
       /* These options don't set a flag.
          We distinguish them by their indices. */
       {"config",  required_argument, 0, 'c'},
       {"userCount",  required_argument, 0, 'u'},
       {"duration",    required_argument, 0, 'd'},
       {"help", no_argument, 0, '?'},
       {"logCookies", no_argument, 0, 'C'},
       {0, 0, 0, 0}
     };
   char *configFilename = NULL;
   Configuration configuration;
   int userCount = 1, duration = 5, logCookies = 0;

   /* getopt_long stores the option index here. */
   int option_index = 0, c;

   while( 1 )
   {
      c = getopt_long (argc, argv, "?Cc:u:d:",
            long_options, &option_index);

      /* Detect the end of the options. */
      if (c == -1)
         break;

      switch (c)
      {
         case 0:
            /* If this option set a flag, do nothing else now. */
            if (long_options[option_index].flag != 0)
               break;
            printf ("option %s", long_options[option_index].name);
            if (optarg)
               printf (" with arg %s", optarg);
            printf ("\n");
            break;

         case 'C':
            logCookies = 1;
            break;

         case 'c':
            configFilename = optarg;
            break;

         case 'd':
            duration = atoi( optarg );
            break;

         case 'u':
            userCount = atoi( optarg );
            break;

         case '?':
            usage();
            return 0;
            /* getopt_long already printed an error message. */
            break;

         default:
            assert( FALSE );
      }
   }

   configuration = config_read( configFilename );

   printf( "Running test with %d simulated users for %d seconds.\n", userCount, duration );

   return webTest_run( configuration, userCount, duration, logCookies );
}

static void usage()
{
   puts( "Usage: stampede --config=<configuration file --duration <seconds> \n"
         "         --userCount=<numer of users> --help\n"
         "\n"
         "Tests HTTP services by calling it, verifying that the results are as expected,\n"
         "and measuring the time the calls took.\n"
         "\n"
         "Arguments:"
         "  -c, --config     The name of the configuration file.\n"
         "  -d, --duration   The number of seconds to run the test.\n"
         "  -u, --userCount  The number of users to simulate.\n"
         "  -C, --logCookies Log the cookies used in the transactions\n"
         "  -?, --help       Show this information.\n" );
}
